package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.Staff;
import com.scu.hotelsystem.service.StaffService;
import com.scu.hotelsystem.util.PageInfo;
import com.scu.hotelsystem.util.Result;
import com.scu.hotelsystem.util.search.StaffSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@Controller
public class StaffController {
    @Autowired
    private StaffService staffService;

    @RequestMapping("/")
    public String d(){
        return "redirect:login";
    }

    @RequestMapping("/login")
    public ModelAndView login(@ModelAttribute("tip") String tip){
        ModelAndView view;
        view = new ModelAndView("login");
        view.addObject("tip", tip);
        return view;
    }

    @RequestMapping(path = "/verifyLogin", method = RequestMethod.POST)
    public ModelAndView verifyLogin(RedirectAttributes redirectAttributes,
                                    HttpSession httpSession,
                                    @RequestParam("account") String account,
                                    @RequestParam("password") String password,
                                    @RequestParam(name = "isAdmin", required = false) boolean isAdmin){
        ModelAndView view;
        int id;

        view = new ModelAndView();
        if(isAdmin){
            view.setViewName("forward:verifyManagerLogin");
        }else {
            id = staffService.verifyLogin(account, password);
            if (id == staffService.FAILED){
                redirectAttributes.addFlashAttribute("tip", "账户或密码错误");
                view.setViewName("redirect:login");
            }else {
                httpSession.setAttribute("staffId", id);
                view.setViewName("redirect:staffIndex");
            }
        }

        return view;
    }

    @RequestMapping("/staffIndex")
    public String staffIndex(){
        return "businessManageIndex";
    }

    @RequestMapping("/staffListPage")
    public String staffListPage(){
        return "stafflist";
    }


    @RequestMapping("/addStaffPage")
    public String addStaffPage(){
        return "index";
    }

    @RequestMapping("/editStaffPage")
    public String editStaffPage(){
        return "index";
    }

    @RequestMapping("/staffDetailPage")
    public String staffDetailPage(int id){
        return "staffdetail";
    }

    @RequestMapping("/searchStaffListRest")
    @ResponseBody
    public Result searchStaffListRest(StaffSearch staffSearch, PageInfo pageInfo){

        List<Staff> staffList;
        Staff search = new Staff(staffSearch);
        staffList = staffService.searchStaffList(search, pageInfo.index(), pageInfo.limit());
        int count = staffList.size();

        return new Result(count, staffList);

    }

    @RequestMapping("/staffEditPage")
    public ModelAndView staffEditPage(StaffSearch staffSearch){
        List<Staff> staffList;
        ModelAndView view;

        view = new ModelAndView("staffedit");
        Staff search = new Staff(staffSearch);
        staffList = staffService.searchStaffList(search, 0, 1);
        if (staffList.size() != 0){
            view.addObject("staff", staffList.get(0));
        }
        return view;

    }

    @RequestMapping("/updateStaff")
    @ResponseBody
    public Result updateStaff(Staff staff){
        int n = 0;
        try {
            n = staffService.updateStaff(staff);
        } catch (SQLIntegrityConstraintViolationException e) {
            return new Result(1, e.getMessage());
        }
        System.out.println(n);
        if (n == 1){
            return new Result();
        }else if (n == 0){
            return new Result(2, "未知失败");
        }else{
            return new Result(3, "未知错误");
        }

    }

    @RequestMapping("/staffAddPage")
    public String staffAddPage(){
        return "staffadd";
    }

    @RequestMapping("/addStaff")
    @ResponseBody
    public Result addStaff(Staff staff){
        int n = 0;
        try {
            n = staffService.addStaff(staff);
        } catch (SQLIntegrityConstraintViolationException e) {
            return new Result(1, e.getMessage());
        }

        if (n == 1 || n == 2){
            return new Result();
        }else{
            return new Result(2, "未知错误");
        }
    }

    @RequestMapping("/deleteStaff")
    @ResponseBody
    public Result deleteStaff(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = staffService.deleteStaff(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }
    }

}
